<template>
	<view
		:class="border ? '' : 'uni-combox__no-border'"
		class="uni-combox"
	>
		<!-- label -->
		<view
			v-if="label"
			:style="labelStyle"
			class="uni-combox__label"
		>
			<text>{{ label }}</text>
		</view>
		<!-- 输入框 -->
		<view class="uni-combox__input-box">
			<input
				v-model="inputVal"
				:placeholder="placeholder"
				class="uni-combox__input"
				placeholder-class="uni-combox__input-plac"
				type="text"
				@blur="onBlur"
				@focus="onFocus"
				@input="onInput"
			/>
			<uni-icons
				:type="showSelector ? 'top' : 'bottom'"
				color="#999"
				size="14"
				@click="toggleSelector"
			/>
		</view>
		<!-- 候选列表 -->
		<view
			v-if="showSelector"
			class="uni-combox__selector"
		>
			<view class="uni-popper__arrow" />
			<scroll-view
				class="uni-combox__selector-scroll"
				scroll-y="true"
				@scroll="onScroll"
			>
				<!-- 没有数据的提示 -->
				<view
					v-if="filterCandidatesLength === 0"
					class="uni-combox__selector-empty"
				>
					<text>{{ emptyTips }}</text>
				</view>
				<!-- 候选项列表 -->
				<view
					v-for="(item, index) in filterCandidates"
					:key="index"
					class="uni-combox__selector-item"
					@click="onSelectorClick(index)"
				>
					<text v-if="optionKey === ''">{{ item }}</text>
					<text v-else>{{ item[optionKey] }}</text>
				</view>
			</scroll-view>
		</view>
		<!-- 新增蒙层，点击蒙层时关闭选项显示 -->
		<view
			v-show="showSelector"
			class="uni-combox__mask"
			@click="showSelector = false"
		/>
	</view>
</template>

<script>
/**
 * Combox 组合输入框
 * @description 组合输入框一般用于既可以输入也可以选择的场景
 * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
 * @property {String} label 左侧文字
 * @property {String} labelWidth 左侧内容宽度
 * @property {String} placeholder 输入框占位符
 * @property {Array} candidates 候选项列表
 * @property {String} optionKey 候选项的要显示的Key
 * @property {String} emptyTips 筛选结果为空时显示的文字
 * @property {String} value 组合框的值
 * @function getCurrentItem 点击候选项时返回给父级当前选中的项目，一般用于 候选列表数据为 对象时获取选中编码时所用
 */
export default {
	name: 'MuCombox',
	emits: ['input', 'update:modelValue', 'getCurrentItem'],
	props: {
		border: {
			type: Boolean,
			default: true
		},
		label: {
			type: String,
			default: ''
		},
		labelWidth: {
			type: String,
			default: 'auto'
		},
		placeholder: {
			type: String,
			default: ''
		},
		candidates: {
			type: Array,
			default() {
				return [];
			}
		},
		emptyTips: {
			type: String,
			default: '无匹配项'
		},
		optionKey: {
			type: String,
			default: ''
		},
		// #ifndef VUE3
		value: {
			type: [String, Number],
			default: ''
		},
		// #endif
		// #ifdef VUE3
		modelValue: {
			type: [String, Number],
			default: ''
		}
		// #endif
	},
	data() {
		return {
			showSelector: false,
			inputVal: '',
			blurTimer: null,
			currentItem: undefined
		};
	},
	computed: {
		labelStyle() {
			if (this.labelWidth === 'auto') {
				return '';
			}
			return `width: ${this.labelWidth}`;
		},
		filterCandidates() {
			if (this.inputVal !== 0 && !this.inputVal) {
				return this.candidates;
			}
			return this.candidates.filter(item => {
				if (this.optionKey === '') {
					return item.toString().indexOf(this.inputVal) > -1;
				} else {
					return item[this.optionKey].toString().indexOf(this.inputVal) > -1;
				}
			});
		},
		filterCandidatesLength() {
			return this.filterCandidates.length;
		}
	},
	watch: {
		// #ifndef VUE3
		value: {
			handler(newVal) {
				this.inputVal = newVal;
			},
			immediate: true
		},
		// #endif
		// #ifdef VUE3
		modelValue: {
			handler(newVal) {
				this.inputVal = newVal;
			},
			immediate: true
		},
		// #endif
		currentItem: {
			handler(newVal) {
				this.inputVal = this.optionKey === '' ? newVal : newVal[this.optionKey];
				this.$emit('getCurrentItem', newVal);
				this.$emit('input', this.inputVal);
				this.$emit('update:modelValue', this.inputVal);
			}
		}
	},
	methods: {
		toggleSelector() {
			this.showSelector = !this.showSelector;
		},
		onFocus() {
			this.showSelector = true;
		},
		onBlur() {
			this.blurTimer = setTimeout(() => {
				this.showSelector = false;
			}, 153);
		},
		onScroll() {
			// 滚动时将blur的定时器关掉
			if (this.blurTimer) {
				clearTimeout(this.blurTimer);
				this.blurTimer = null;
			}
		},
		onSelectorClick(index) {
			this.showSelector = false;
			this.currentItem = this.filterCandidates[index];
			this.$emit('input', this.inputVal);
			this.$emit('update:modelValue', this.inputVal);
		},
		onInput() {
			setTimeout(() => {
				this.$emit('input', this.inputVal);
				this.$emit('update:modelValue', this.inputVal);
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.uni-combox {
	font-size: 14px;
	border: 1px solid #dcdfe6;
	border-radius: 4px;
	padding: 6px 10px;
	position: relative;
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	// height: 40px;
	flex-direction: row;
	align-items: center;
	// border-bottom: solid 1px #DDDDDD;
}

.uni-combox__label {
	font-size: 16px;
	line-height: 22px;
	padding-right: 10px;
	color: #999999;
}

.uni-combox__input-box {
	position: relative;
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex: 1;
	flex-direction: row;
	align-items: center;
}

.uni-combox__input {
	flex: 1;
	font-size: 14px;
	height: 22px;
	line-height: 22px;
}

.uni-combox__input-plac {
	font-size: 14px;
	color: #999999;
}

.uni-combox__selector {
	/* #ifndef APP-NVUE */
	box-sizing: border-box;
	/* #endif */
	position: absolute;
	top: calc(100% + 12px);
	left: 0;
	width: 100%;
	background-color: #ffffff;
	border: 1px solid #ebeef5;
	border-radius: 6px;
	box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	z-index: 3;
	padding: 4px 0;
}

.uni-combox__selector-scroll {
	/* #ifndef APP-NVUE */
	max-height: 200px;
	box-sizing: border-box;
	/* #endif */
}

.uni-combox__selector-empty,
.uni-combox__selector-item {
	/* #ifndef APP-NVUE */
	display: flex;
	cursor: pointer;
	/* #endif */
	line-height: 36px;
	font-size: 14px;
	text-align: center;
	// border-bottom: solid 1px #DDDDDD;
	padding: 0px 10px;
}

.uni-combox__selector-item:hover {
	background-color: #f9f9f9;
}

.uni-combox__selector-empty:last-child,
.uni-combox__selector-item:last-child {
	/* #ifndef APP-NVUE */
	border-bottom: none;
	/* #endif */
}

// picker 弹出层通用的指示小三角
.uni-popper__arrow,
.uni-popper__arrow::after {
	position: absolute;
	display: block;
	width: 0;
	height: 0;
	border-color: transparent;
	border-style: solid;
	border-width: 6px;
}

.uni-popper__arrow {
	filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
	top: -6px;
	left: 10%;
	margin-right: 3px;
	border-top-width: 0;
	border-bottom-color: #ebeef5;
}

.uni-popper__arrow::after {
	content: ' ';
	top: 1px;
	margin-left: -6px;
	border-top-width: 0;
	border-bottom-color: #ffffff;
}

.uni-combox__no-border {
	border: none;
}

.uni-combox__mask {
	width: 100%;
	height: 100%;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 1;
}
</style>
